const { typeJson } = require('../../utils/typejson')
const { get, post } = require('../../request/index')

Component({
  /**
   * 组件的属性列表
   */
  properties: {

    net: {
      type: Object,
      observer: function (newVal, oldVal, changedPath) {
        this.getList(newVal)
      }
    },

    value: {
      type: Number
    }

  },



  /**
   * 组件的初始数据
   */
  data: {
    display: "",
    show: false,
    searchValue: '',
    viewShow: true,
    searchList: [
      { key: 3, value: "d" }
    ],
    searchListStore: []

  },

  /**
   * 组件的方法列表
   */
  methods: {


    onChange(e) {
      let txt = e.detail;
      if (txt == "") {
        this.setData({
          searchList: this.data.searchListStore
        })
        return
      }
      let f = this.data.searchListStore.filter(function (s) {
        return s.key.indexOf(txt) > -1
      })
      this.setData({
        searchList: f
      })
    },

    async getList(net) {

      const { url, method, key, value, data } = net
      if (data.mineId == true) {
        let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
        data.mineId = prolist.mineId
      }
      var res = ""
      if (method == 'get') {
        res = await get(url + "?currentPage=1&pageSize=999", data)
      } else {
        res = await post(url + "?currentPage=1&pageSize=999", data)
      }
      let list = []
      if (res.rows) {
        list = res.rows
      } else {
        list = res.data.list
      }
      var tempList = []
      list.map(x => {
        let temp = { ...x, key: x[key] }
        tempList.push(temp)
      })

      this.setData({
        searchList: tempList,
        searchListStore: tempList
      })


      if (this.properties.value) {
        tempList.map(x => {
          if (x.value == this.properties.value) {
            this.setData({
              display: x.key
            })
          }
        })
      }

    },


    close() {
      this.setData({
        show: false
      })
    },

    bindtap() {
      this.setData({
        show: true
      })
    },

    bindChange(e) {
      let obj = e.target.dataset;

      this.setData({
        display: obj.key,
        show: false
      })
      this.triggerEvent('change', obj.value);

    }

  }
})
